<?php
class TagsController extends TaggingAppController {
    public $name = 'Tags';
    public $components = array('RequestHandler');
    public $helpers = array('Html', 'Form', 'Javascript', 'Tagging.Tagging');
    public $paginate = array(
        'Tag' => array(
            'order' => 'Tag.name ASC',
            'limit' => 20,
            'recursive' => -1
        ),
        'Tagged' => array(
            'fields' => array('Tagged.model', 'Tagged.model_id'),
            'order' => 'Tagged.id DESC',
            'limit' => 10
        )
    );

    /**
     * check for tags base on the provided content
     */
    public function auto(){
        $matches = array();
        $this->layout = 'ajax';
        if($this->RequestHandler->isAjax() && $this->RequestHandler->isPost()){
            App::import('Core', 'Sanitize');
            $content = Sanitize::clean($this->params['form']['content']);
            $matches = $this->Tag->auto($content);
        }
        $this->set(compact('matches'));
    }

    public function index(){
        $this->set('tags', $this->paginate());
    }

    /**
     * All Tags used at least once
     * You have to create a view for this action in {your_app}/views/plugins/tagging/tags/index.ctp
     *
     * Available variables in view :
     * $data : all used tags ordered by name ASC
     */
    public function cloud(){
        $clouds = $this->Tag->tagCloud();
        if(isset($this->params['requested'])){
            return $clouds;
        }
        $this->layout = 'ajax';
        $this->set(compact('clouds'));
    }

    /**
     * View Tag
     * Checks $this->params['pass'] for slug or id
     * You have to create a view for this action in {your_app}/views/plugins/tagging/tags/view.ctp
     *
     * Available variables in view :
     * $tag : Tag data
     * $data : paginated ressources tagged with this tag
     */
    public function view(){
        if(!isset($this->params['pass'][0])){
            $this->cakeError('error404', array(array('url' => $this->action)));
        }
        $param = $this->params['pass'][0];

        if(preg_match('/^\d+$/', $param)){
            $findMethod = 'findById';
        }else{
            $findMethod = 'findBySlug';
        }

        $this->Tag->recursive = -1;

        if(!$tag = $this->Tag->{$findMethod}($param)){
            $this->cakeError('error404', array(array('url' => $this->action)));
        }

        $tagged = $this->paginate('Tagged', array('Tag.id' => $tag['Tag']['id']));

        // Build $data with actual Models data
        $data = array();

        foreach($tagged as $row){
            $data[] = ClassRegistry::init($row['Tagged']['model'])->read(null, $row['Tagged']['model_id']);
        }

        $this->set(compact('tag', 'data'));
    }

    public function add(){
        if(!empty($this->data)){
            $this->Tag->create();
            if($this->Tag->save($this->data)){
                $this->Session->setFlash(__d('tagging', 'The Tag has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__d('tagging', 'The Tag could not be saved. Please, try again.', true));
            }
        }
    }

    public function delete($id = null){
        if(!$id){
            $this->Session->setFlash(__d('tagging', 'Invalid id for Tag', true));
        }
        if($this->Tag->del($id)){
            $this->Session->setFlash(__d('tagging', 'Tag deleted', true));
        }
        $this->redirect(array('action' => 'index'));
    }
}
?>